package com.shameit.goodqrinfomanager.ui.activity.chuku;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanDevice;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.shameit.goodqrinfomanager.MyApplication;
import com.shameit.goodqrinfomanager.adapter.ChukuAdapter;
import com.shameit.goodqrinfomanager.bean.litepal.RuKu;
import com.shameit.goodqrinfomanager.bean.litepal.XiangMa;
import com.shameit.goodqrinfomanager.source.http.DataRepository;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ChukuViewModel extends BaseViewModel<DataRepository> {
    private static final String TAG = "ChukuViewModel";
    private final static String SCAN_ACTION = "scan.rcv.message";
    private String barcodeStr;
    private StringBuffer sb;
    private List<String> productList;

    private ScanDevice sm;

    public ChukuAdapter adapter;

    public ObservableField<String> cwbh = new ObservableField<>("");
    public ObservableField<String> xiangma = new ObservableField<>("");

    public ChukuViewModel(@NonNull Application application, DataRepository model) {
        super(application, model);
        sb = new StringBuffer();
        productList = new ArrayList<>();
    }

    public List<RuKu> getXiangma(String cangwei){
        return LitePal.where("cangweiCode = ?", cangwei).order("xiangCode").find(RuKu.class);
    }

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            byte[] barocode = intent.getByteArrayExtra("barocode");
            int barocodelen = intent.getIntExtra("length", 0);
            byte temp = intent.getByteExtra("barcodeType", (byte) 0);
            byte[] aimid = intent.getByteArrayExtra("aimid");
            barcodeStr = new String(barocode, 0, barocodelen);

            xiangma.set(barcodeStr);

            RuKu ruKu = new RuKu();
            ruKu.setCangweiCode(cwbh.get());
            ruKu.setOperator("unknow");
            ruKu.setOperatorTime(MyApplication.yyHHMMddhhmmss.format(new Date()));
            ruKu.setUserCode(MyApplication.loginUser.getUsername());
            ruKu.setXiangCode(barcodeStr);
            ruKu.save();
            sm.stopScan();
//            adapter.notifyDataSetChanged();
            adapter.setNewInstance(getXiangma(cwbh.get()));
        }
    };

    public BindingCommand startOneScan = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            sm.stopScan();
            sm.openScan();
            sm.setScanLaserMode(8);
            sm.startScan();
        }
    });

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        super.onCreate();
        sm = new ScanDevice();
        sm.setOutScanMode(0);//启动就是广播模式
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        Log.d(TAG, "onResume: ");
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        getApplication().registerReceiver(mScanReceiver, filter);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        Log.d(TAG, "onPause: ");
        getApplication().unregisterReceiver(mScanReceiver);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        Log.d(TAG, "onStop: ");
        sm.stopScan();
    }

    public void onDestroy(){
        Log.d(TAG, "onDestroy: ");
        if (sm != null) {
            sm.stopScan();
            sm.setScanLaserMode(8);
            sm.closeScan();
        }
    }

}
